package praktikumid.p05_k;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.MalformedURLException;
import java.net.URL;
import java.net.URLConnection;

/**
 * Comments!
 * @author ago
 *
 */
public class FeedBeta {

	/**
	 * Translate into language.
	 */
	public static String lang = "en";
	
	public static final String GOOGLE_GROUPS_URL = "https://groups.google.com/group/iti0011/feed/rss_v2_0_msgs.xml";
	/**
	 * @param args
	 */
	public static void main(String[] args) {
		
		/*
		 * todolist:
		 * - parameetrite parsimine. saame tõlkekeele
		 *   või näita juhendit
		 * - teeme päringu google groupsi
		 * - parsime XML-i
		 * - teeme andme objekti
		 * - tõlgime title ja description väljad
		 *   - teeme päringu google translate'i
		 *   - parsime tulemusest tõlke
		 *   - asendame objektis vastavad väljad
		 * - optional: sortimine
		 * - kuvame kõik andmeobjektid välja
		 * 
		 */

		//parseArguments(args);
		FeedBeta.parseArguments(args);
		
		System.out.println("lang: " + lang);
		String xml = readGoogleGroups();
		GoogleGroupItem i = parseGoogleGroups(xml);
		System.out.println("item.title:" + i.title);
		
	}
	
	/**
	 * Prints out help.
	 */
	public static void showHelp() {
		System.out.println("Usage: xxx yyy");
		System.out.println("Options:");
		System.out.println("      -h    Show help");
		System.out.println("      -l    language");
		System.exit(0);
	}
	
	public static void parseArguments(String[] args) {
		/*
		 * parameetrid:
		 * -h 
		 * --help
		 * -l en (l=en)
		 * --lang=en (lang=en)
		 * 
		 * en
		 * 
		 * java Feed en 7 desc
		 * java Feed desc en 7
		 * 
		 * java Feed --sort=desc --cache=7 --lang=ru
		 * java Feed -s -l ru -c 7
		 */
		System.out.println("length:" + args.length);
		for (int i = 0; i < args.length; i++) {
			System.out.println("arg[" + i + "] = " + args[i]);
		}
		
		for (String arg : args) {
			/*
			 * String.indexOf - otsitava sõna positioon
			 * algses sõnas.
			 * 
			 * "tere tulemast".indexOf("t") = 0
			 * "tere tulemast".indexOf("te") = 0
			 * "tere tulemast".indexOf("tu") = 5
			 * "tere tulemast".indexOf("ta") = -1
			 * 
			 * 
			 * String.substring - lõikab väiksema alamstringi
			 *  012345678   <-- indeks ehk positsioon stringis
			 * "tere tulemast".substring(0, 1) = "t"
			 * "tere tulemast".substring(0, 4) = "tere"
			 * "tere tulemast".substring(6, 8) = "ul"
			 * 
			 * a--lang
			 * --language
			 * 0123456789
			 * --lang=XXXX
			 * 
			 * 
			 */
			String param = "--lang=";
			if (arg.indexOf(param) == 0) {
				lang = arg.substring(param.length());
			}
			if (arg.indexOf("--help") == 0) {
				showHelp();
			}
		}
		
	}
	
	public static String readGoogleGroups() {
		try {
			URL url = new URL(GOOGLE_GROUPS_URL);
			URLConnection c = url.openConnection();
			c.setRequestProperty("User-agent", "Mozilla/5.0");
			BufferedReader br = new BufferedReader(
					new InputStreamReader(
							c.getInputStream()));
			
			String line;
			String xml = "";
			while ((line = br.readLine()) != null) {
				//if (line == null) break;
				//System.out.println(line);
				xml += line;
			}
			return xml;
		} catch (MalformedURLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return null;
	}

	public static GoogleGroupItem parseGoogleGroups(String xml) {
		//System.out.println(xml);
		// xml.indexOf("<item>", eelmiseItemiL�ppPos);
		// TODO: tee eraldi meetod elemendi sisu parsimiseks
		int itemStart = xml.indexOf("<item>");
		int itemEnd = xml.indexOf("</item>");
		itemEnd += "</item>".length();
		String itemXml = xml.substring(itemStart, itemEnd);
		//System.out.println(itemXml);
		GoogleGroupItem i = parseItem(itemXml);
		return i;
		
	}
	
	public static GoogleGroupItem parseItem(String itemXml) {
		String tag = "title";
		String startTag = "<" + tag + ">";
		String endTag = "</" + tag + ">";
		int startPos = itemXml.indexOf(startTag);
		// me ei taha <title> täägi pealkirja sisse,
		// alustame kaugemalt
		startPos += startTag.length();
		int endPos = itemXml.indexOf(endTag);
		String value = itemXml.substring(startPos, endPos);
		System.out.println(value);
		
		GoogleGroupItem item = new GoogleGroupItem();
		item.title = value;
		return item;
		/*
		item.title = "title";
		item.description = "blah";
		GoogleGroupItem.staatik = "t";
		
		GoogleGroupItem i2 = new GoogleGroupItem();
		i2.title = "title2";
		i2.title = "title3";
		GoogleGroupItem.staatik = "l";
		*/
	}
}
